//
//  FloatingWindow.swift
//  
//
//  Created by Jason on 2021/8/10.
//

import Foundation
import UIKit


class ST_SwiftyFloatingWindow: UIWindow {
    
    /// 小窗还原 block
    var tap_recover_closure:(()->())?
    
    fileprivate let space: CGFloat = ST_SwiftyWidgetConfig.default.windowMinimumSpace
    
    func show() {
        
        self.windowLevel = UIWindow.Level.statusBar + 1.0
        self.screen = UIScreen.main
        self.isHidden = false
        
        let panGesture = UIPanGestureRecognizer.init(target: self, action: #selector(didPan(_:)))
        self.addGestureRecognizer(panGesture)
        
        let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(didTap(_:)))
        self.addGestureRecognizer(tapGesture)
    }
    
    @objc fileprivate func didTap(_ tapGesture: UITapGestureRecognizer) {
        
        tap_recover_closure?()
        
    }
    
    @objc fileprivate func didPan(_ panGesture: UIPanGestureRecognizer) {
        let point = panGesture.translation(in: panGesture.view)
        var originX = self.frame.origin.x + point.x
        if originX < space {
            originX = space
        } else if originX > UIScreen.main.bounds.width - ST_SwiftyWidgetConfig.default.floatingWindowRadious - space {
            originX = UIScreen.main.bounds.width - ST_SwiftyWidgetConfig.default.floatingWindowRadious - space
        }
        var originY = self.frame.origin.y + point.y
        if originY < space {
            originY = space
        } else if originY > UIScreen.main.bounds.height - ST_SwiftyWidgetConfig.default.floatingWindowRadious - space {
            originY = UIScreen.main.bounds.height - ST_SwiftyWidgetConfig.default.floatingWindowRadious - space
        }
        self.frame = CGRect.init(x: originX, y: originY, width: self.bounds.width, height: self.bounds.height)
        if panGesture.state == UIGestureRecognizer.State.cancelled || panGesture.state == UIGestureRecognizer.State.ended || panGesture.state == UIGestureRecognizer.State.failed {
            self.adjustFrameAfterPan()
        }
        panGesture.setTranslation(CGPoint.zero, in: self)
    }
    
    fileprivate func adjustFrameAfterPan() {
        var originX: CGFloat = space
        if self.center.x < UIScreen.main.bounds.width / 2 {
            originX = space
        } else if self.center.x >= UIScreen.main.bounds.width / 2 {
            originX = UIScreen.main.bounds.width - space - self.frame.size.width
        }
        UIView.animate(withDuration: 0.25, animations: {
            self.frame = CGRect.init(x: originX, y: self.frame.origin.y, width: self.frame.size.width, height: self.frame.size.height)
        }) { (complete) in
            //      FloatingTool.setLatestOrigin(origin: self.frame.origin)
        }
    }
    
    @available(iOS 13, *)
    class func keyWindowScene() -> UIWindowScene? {
        return UIApplication.shared.connectedScenes
            .filter { $0.activationState == .foregroundActive }
            .first as? UIWindowScene
    }
    
}
